package org.madmac.report;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;

import org.madmac.exception.GenerateScriptErrorException;
import org.madmac.maquina.Maquina;

public class ReportHardwareMaquina extends Report {

	public boolean generatePdf(Maquina maquina) throws GenerateScriptErrorException {

		boolean works = false;

		try {
         
			Document document = new Document();
			PdfWriter.getInstance(document,
				new FileOutputStream(this.realpath.concat(this.outputFilename)));
			document.open();

			Date data = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
			String dataAtual = sdf.format(data);

			HeaderFooter header = new HeaderFooter(new Phrase(this.outputFilename + "  -  " + dataAtual), false);
			HeaderFooter footer = new HeaderFooter(
				new Phrase("MAD MAC 1.0.1  -  "), true);
			header.setAlignment(1);
			header.setBorderWidthTop(0.2f);
			header.setBorderWidthBottom(0.2f);

			footer.setAlignment(2);
			footer.setBorderWidthTop(0.2f);
			footer.setBorderWidthBottom(0.2f);

			Chapter chapter = new Chapter("MAD MAC 1.0.1     .:.    IF - Campus Maracanaú", 1);
			Paragraph paragraph = new Paragraph(maquina.getModelo());
			chapter.add(paragraph);

			document.setHeader(header);
			document.setFooter(footer);
			document.add(chapter);
			document.close();

			works = true;

		} catch (Exception e) {
			throw new GenerateScriptErrorException("Erro ao gerar arquivo PDF.");
		}

		return works;
	}
}
